package com.servlet;

import com.bean.Building;
import com.google.gson.Gson;
import com.service.impl.BuildingServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/BuildingServlet/*")
public class BuildingServlet extends HttpServlet {

    BuildingServiceImpl buildingService = new BuildingServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html,charset=utf-8");//命令浏览器用什么编码打开文件

        String pathInfo = request.getPathInfo().substring(1);
        if("listAll".equals(pathInfo)){
            listAll(request,response);
        }else if("deleteById".equals(pathInfo)){
            deleteById(request,response);
        }else if("removeAll".equals(pathInfo)){
            removeAll(request,response);
        }else if("listAllByPagination".equals(pathInfo)){
            listAllByPagination(request,response);
        }else if("buildAdd".equals(pathInfo)){
           buildAdd(request,response);
        }else if("updateById".equals(pathInfo)){
            updateById(request,response);
        }else if("listAllByPaginationSearch".equals(pathInfo)){
            listAllByPaginationSearch(request,response);
        }
    }

    private void listAllByPaginationSearch(HttpServletRequest request, HttpServletResponse response) {
        int offset = Integer.valueOf(request.getParameter("offset"));
        int limit = Integer.valueOf(request.getParameter("limit"));
        String search = request.getParameter("search");


        try {
            List<Building> buildingList = buildingService.selectAllPaginationSearch(offset,limit,search);

            int total = buildingService.countSearch(search);
            System.out.println(total);
            Map<String,Object> map = new HashMap<>();
            map.put("total",total);
            map.put("rows",buildingList);

            Gson gson = new Gson();
            String s =gson.toJson(map);
            response.getWriter().print(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void updateById(HttpServletRequest request, HttpServletResponse response) {
        Integer buildId = Integer.valueOf(request.getParameter("buildId"));
        String buildName = request.getParameter("buildName");
        String buildConstruct = request.getParameter("buildConstruct");
        Integer buildUnits = Integer.valueOf(request.getParameter("buildUnits"));
        Integer buildFloors = Integer.valueOf(request.getParameter("buildFloors"));
        Integer floorRooms = Integer.valueOf(request.getParameter("floorRooms"));
        String buildDesc = request.getParameter("buildDesc");
        String buildTime = request.getParameter("buildTime");
        Building building = new Building(buildId,buildName,buildConstruct,buildUnits,buildFloors,floorRooms,buildDesc,buildTime);
        try {
            boolean b = buildingService.update(building);
            if(b){
                response.getWriter().print("success");
            }else{
                response.getWriter().print("");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void buildAdd(HttpServletRequest request, HttpServletResponse response) {
        String buildName = request.getParameter("buildName");
        String buildConstruct = request.getParameter("buildConstruct");
        Integer buildUnits = Integer.valueOf(request.getParameter("buildUnits"));
        Integer buildFloors = Integer.valueOf(request.getParameter("buildFloors"));
        Integer floorRooms = Integer.valueOf(request.getParameter("floorRooms"));
        String buildDesc = request.getParameter("buildDesc");
        String buildTime = request.getParameter("buildTime");

        Building building = new Building(null,buildName,buildConstruct,buildUnits,buildFloors,floorRooms,buildDesc,buildTime);
        try {
            boolean b = buildingService.insert(building);
            if(b){
                request.setAttribute("msg","新增成功");
            }else{
                request.setAttribute("msg","新增失败");
            }
            request.getRequestDispatcher("/property/building_add.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void listAllByPagination(HttpServletRequest request, HttpServletResponse response) {
        int offset = Integer.valueOf(request.getParameter("offset"));
        int limit = Integer.valueOf(request.getParameter("limit"));

        try {
            List<Building> buildingList = buildingService.selectAllPagination(offset,limit);
            int total = buildingService.count();
            Map<String,Object> map = new HashMap<>();
            map.put("total",total);
            map.put("rows",buildingList);

            Gson gson = new Gson();
            String s =gson.toJson(map);
            response.getWriter().print(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void removeAll(HttpServletRequest request, HttpServletResponse response) {
        String ids = request.getParameter("ids");
        System.out.println(ids);
        String[] idsArr = ids.split(",");
        try {
            boolean b = buildingService.removeAll(idsArr);
            if(b){
                response.getWriter().print("删除成功");
            }else{
                response.getWriter().print("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void deleteById(HttpServletRequest request, HttpServletResponse response) {
        Integer buildId = Integer.valueOf(request.getParameter("buildId"));
        try {
            boolean b = buildingService.deleteById(buildId);
            if(b){
                response.getWriter().print("删除成功");
            }else{
                response.getWriter().print("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void listAll(HttpServletRequest request, HttpServletResponse response) {

        try {
            List<Building> buildingList = buildingService.selectAll();

            Gson gson = new Gson();
            String s = gson.toJson(buildingList);
            response.getWriter().print(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
